package com.source.lightfowl.basedata.service.impl;

import com.source.lightfowl.basedata.dao.BarcodeMachineDao;
import com.source.lightfowl.basedata.dao.BarcodeMachineStatusDao;
import com.source.lightfowl.basedata.dao.StallsDao;
import com.source.lightfowl.basedata.entity.BarcodeMachine;
import com.source.lightfowl.basedata.entity.Barcode_Machine_Status;
import com.source.lightfowl.basedata.entity.Stalls;
import com.source.lightfowl.basedata.service.BarcodeMachineMsgService;
import com.source.lightfowl.basedata.service.BarcodeMachineStatusService;
import com.source.lightfowl.basedata.service.StallsService;
import com.source.lightfowl.person.dao.PersonDao;
import com.source.lightfowl.person.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class BarcodeMachineStatusServiceImpl implements BarcodeMachineStatusService {

    @Autowired
    private BarcodeMachineStatusDao barcodeMachineStatusDao;
    @Autowired
    private StallsService stallsService;
    @Autowired
    private BarcodeMachineDao barcodeMachineDao;
    @Autowired
    private PersonDao personDao;

    @Autowired
    private BarcodeMachineMsgService barcodeMachineMsgService;

    @Override
    public void editBarcodeMachineStatus(Barcode_Machine_Status barcode_machine_status) {
        barcodeMachineStatusDao.save(barcode_machine_status);
    }

    @Override
    public List<Barcode_Machine_Status> findBarcodeMachineStatus() {

        //1.查询全部数据
        List<Barcode_Machine_Status> all = barcodeMachineStatusDao.findAll();

        //2.查询冗余数据 前端需要展示
        List<Barcode_Machine_Status> collect2 = all.stream().map(s -> {
            //to do
            String barcodeMachineId = s.getBarcodeMachineId();
            Stalls stalls = stallsService.findByBarcodeMachineId(barcodeMachineId);
            if(stalls == null){
                //
                System.out.println("根据脚标机id查找档口出错  没有数据  脚标机id："+barcodeMachineId);
            }else {
                s.setStallsName(stalls.getStallsName());

                s.setLinkman(stalls.getContacts());
                String userId = stalls.getUserId();
                Optional<Person> byId = personDao.findById(userId);
                if (byId.isPresent()){
                    s.setPhome(byId.get().getPhone());
                    s.setUserName(byId.get().getName());
                }
            }

            return s;
        }).collect(Collectors.toList());

        //排序 方便查看 相同id的状态
        Collections.sort(collect2, Comparator.comparing(Barcode_Machine_Status::getBarcodeMachineId));
        return collect2;

    }

    @Override
    public Page<Barcode_Machine_Status> findPageStatus(int page, int size) {
        return barcodeMachineStatusDao.findAll(PageRequest.of(page-1, size));
    }

    @Override
    public void deleteBarcodeMachineStatus(String id) {
        barcodeMachineStatusDao.deleteById(id);
        //barcodeMachineMsgService.cascadingDeleteById(id);//级联删除
    }

    @Override
    public void deleteByBarcodeMachineId(String id) {
        barcodeMachineStatusDao.deleteByBarchodeMachineId(id);
    }

    @Override
    public Barcode_Machine_Status findBarcodeMachineStatusById(String id) {
        Barcode_Machine_Status s = barcodeMachineStatusDao.findById(id).get();


        String barcodeMachineId = s.getBarcodeMachineId();
        Stalls stalls = stallsService.findByBarcodeMachineId(barcodeMachineId);
        s.setStallsName(stalls.getStallsName());
        s.setLinkman(stalls.getContacts());
        String userId = stalls.getUserId();

        Optional<Person> byId = personDao.findById(userId);
        if (byId.isPresent()){
            s.setPhome(byId.get().getPhone());
            s.setUserName(byId.get().getName());
        }

        return s;
    }

    @Override
    public Barcode_Machine_Status findBarcodeMachineStatusByMachineId(String id) {
        return barcodeMachineStatusDao.getByBarchodeMachineId(id);
    }
}
